{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "229b9828",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d70769a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def all_keys(my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: Collects all keys from the dictionary and stores them in a list\n",
    "    my_dict: The question dictionary\n",
    "    Return: returns the list created\n",
    "    \"\"\"\n",
    "    question_list = []\n",
    "    for key in my_dict.keys():\n",
    "        question_list.append(key)\n",
    "    return question_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16e587d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def question_details(key, my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: Compacts the question details from the dictionary\n",
    "    kes: the key \n",
    "    my_dict: the dictionary\n",
    "    Return: returns question details in a list\n",
    "    \"\"\"\n",
    "    question = my_dict[key][\"questionText\"]\n",
    "    opt1 = my_dict[key][\"option1\"]\n",
    "    opt2 = my_dict[key][\"option2\"]\n",
    "    opt3 = my_dict[key][\"option3\"]\n",
    "    opt4 = my_dict[key][\"option4\"]\n",
    "    answer = my_dict[key][\"answer\"]\n",
    "    attempted = my_dict[key][\"attempted\"]\n",
    "    return [question, opt1, opt2, opt3, opt4, answer, attempted]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "00cc5cb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def check_if_answered(key, my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: The function checks if a user has answered a specific question. \n",
    "    Helps to make sure that a question is not asked twice to the user in the same session\n",
    "    key: the key\n",
    "    my_dict: the dictionary\n",
    "    Return: returns True or False. True means the question is already answered,\n",
    "    \"\"\"\n",
    "    if int(my_dict[key][\"attempted\"]):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "    def mark_attempted(key, my_dict):\n",
    "    \"\"\"\n",
    "    Purpose: The function changes the value of the attempted property in the dictionary.\n",
    "    A question marked as attempted cannot be asked again\n",
    "    key: the key \n",
    "    my_dict: the dictionary\n",
    "    Return: returns None\n",
    "    \"\"\"\n",
    "    my_dict[key][\"attempted\"] = 1\n",
    "\n",
    "    def shuffle_quiz(dictionary):\n",
    "    \"\"\"\n",
    "    Purpose: Shuffles the keys list and selects one randomly before fetching key details from the dictionary\n",
    "    dictionary: the dictionary (default=question_bank)\n",
    "    Return: returns a question from the dict question_array\n",
    "    \"\"\"\n",
    "    all_keys = list(question_bank.keys())\n",
    "    random.shuffle(all_keys)\n",
    "    shuffled_bank = {}\n",
    "    for key in all_keys:\n",
    "        shuffled_bank[key] = question_bank[key]\n",
    "    return shuffled_bank\n",
    "\n",
    "def read_data(filename):\n",
    "    question_bank = {}\n",
    "    f = open(filename,\"r\",encoding='utf-8')\n",
    "    questions = list(csv.reader(f))\n",
    "    for question in questions[1:]:\n",
    "        question_detail = {}\n",
    "        for i in range(1,len(question)):\n",
    "            question_detail[questions[0][i]] = question[i]\n",
    "        question_bank[question[0]] = question_detail\n",
    "    return question_bank\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    flag = True\n",
    "    scores = 0\n",
    "    #read data from csv format file\n",
    "    question_bank = read_data(\"about_china.csv\")\n",
    "    while flag:\n",
    "        choice = input(\"Thank you for your interest in this exam.\\nhint: press QUIT to quit (ABCD or abcd for your choice).\\nPress Enter to continue:\\n>>\")\n",
    "        if choice == 'QUIT':\n",
    "            flag = False\n",
    "        else:\n",
    "            print(\"You have one chance per question\\n\\\n",
    "You cannot go back to answer a previous question\\n\\\n",
    "The total number of questions is 10\\n\\\n",
    "Press any key to start\\n\\\n",
    "QUIZ STARTS NOW\\n\\\n",
    "........................\\n\")\n",
    "            answer_map = {\"a\": 0,\"A\": 0,\"b\": 1,\"B\": 1,\"c\": 2,\"C\": 2,\"d\": 3,\"D\": 3}\n",
    "            correct_list = [\"A\",\"B\",\"C\",\"D\"]\n",
    "            #one session have 10 questions\n",
    "            for i in range(10):\n",
    "                #shuffle the question\n",
    "                shuffled_bank = shuffle_quiz(question_bank)\n",
    "                #get the key of question bank\n",
    "                question_list=all_keys(shuffled_bank)\n",
    "                #check the question answered or not\n",
    "                for key in question_list:\n",
    "                    if not check_if_answered(key, question_bank):\n",
    "                        details = question_details(key,shuffled_bank)\n",
    "                        #print out the question and items\n",
    "                        print(details[0] + \"\\n\" + \"A.\" + details[1] + \"\\n\" + \"B.\" + details[2] + \"\\n\"+ \"C.\" + details[3] + \"\\n\"+ \"D.\" + details[4] + \"\\n\")\n",
    "                        item = input(\">>\")\n",
    "                        #require the user enter correctly\n",
    "                        while True:\n",
    "                            if item in \"ABCDabcd\":\n",
    "                                break\n",
    "                            else:\n",
    "                                print(\"please enter correctly!\\n\")\n",
    "                                item = input(\">>\")\n",
    "                        #check the answer\n",
    "                        if details[1:5].index(details[5]) == answer_map[item]:\n",
    "                            scores += 1\n",
    "                            print(\"Congratulations! your answer is correct!\")\n",
    "                        else:\n",
    "                            print(\"Wrong! The correct answer is \"+correct_list[details[1:5].index(details[5])]+\". \"+details[5])\n",
    "                        question_bank[\"attempted\"] = \"1\"\n",
    "    print(\"Your score is: \",scores,\"Your correct rate is: \",scores/10)\n",
    "    print(\"You are welcome to take the test anytime. Bye!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f52db1ba",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4e8b2696",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4bcd0c94",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e934e253",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'read_data' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-faf72b4f008b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mscores\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[1;31m#read data from csv format file\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mquestion_bank\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mread_data\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"about_china.csv\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[1;32mwhile\u001b[0m \u001b[0mflag\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m         \u001b[0mchoice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Thank you for your interest in this exam.\\nhint: press QUIT to quit (ABCD or abcd for your choice).\\nPress Enter to continue:\\n>>\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'read_data' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32761816",
   "metadata": {},
   "outputs": [],
   "source": [
    "a=[1,2,3,4]\n",
    "a[1:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5358d5e7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
